<template>
  <div class="ai-customer-service">
    <!-- 聊天按钮 -->
    <transition name="bounce">
      <div 
        v-show="!isOpen" 
        class="chat-button" 
        @click="toggleChat"
        title="AI智能客服"
      >
        <i class="bi bi-robot"></i>
        <div class="pulse-ring"></div>
      </div>
    </transition>

    <!-- 聊天窗口 -->
    <transition name="slide-fade">
      <div v-show="isOpen" class="chat-window">
        <!-- 聊天窗口头部 -->
        <div class="chat-header">
          <div class="header-content">
            <i class="bi bi-robot"></i>
            <span class="header-title">AI智能客服</span>
          </div>
          <div class="header-actions">
            <button 
              class="minimize-btn" 
              @click="toggleChat"
              title="最小化"
            >
              <i class="bi bi-dash-lg"></i>
            </button>
            <button 
              class="close-btn" 
              @click="closeChat"
              title="关闭"
            >
              <i class="bi bi-x-lg"></i>
            </button>
          </div>
        </div>

        <!-- RAGflow iframe -->
        <div class="chat-content">
          <iframe
            :src="chatUrl"
            frameborder="0"
            class="chat-iframe"
            @load="onIframeLoad"
          ></iframe>
          <div v-if="isLoading" class="loading-overlay">
            <div class="loading-spinner">
              <i class="bi bi-arrow-repeat"></i>
              <span>正在连接智能客服...</span>
            </div>
          </div>
        </div>
      </div>
    </transition>
  </div>
</template>

<script>
export default {
  name: 'AICustomerService',
  props: {
    // RAGflow共享ID
    sharedId: {
      type: String,
      default: 'ec1da38caef211f088ef72ae004f33d4'
    },
    // 认证token
    authToken: {
      type: String,
      default: 'Y3NjMyMmJhYjNmOTExZjBiMGY2MzJhNz'
    },
    // RAGflow服务地址
    baseUrl: {
      type: String,
      default: 'http://localhost'
    },
    // 语言设置
    locale: {
      type: String,
      default: 'zh'
    }
  },
  data() {
    return {
      isOpen: false,
      isLoading: false
    };
  },
  computed: {
    chatUrl() {
      return `${this.baseUrl}/next-chats/share?shared_id=${this.sharedId}&from=chat&auth=${this.authToken}&locale=${this.locale}`;
    }
  },
  methods: {
    toggleChat() {
      this.isOpen = !this.isOpen;
      if (this.isOpen) {
        this.isLoading = true;
      }
    },
    closeChat() {
      this.isOpen = false;
    },
    onIframeLoad() {
      this.isLoading = false;
    }
  }
};
</script>

<style scoped>
.ai-customer-service {
  position: fixed;
  z-index: 9999;
}

/* 聊天按钮样式 */
.chat-button {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(102, 126, 234, 0.4);
  transition: all 0.3s ease;
  overflow: visible;
}

.chat-button:hover {
  transform: scale(1.1);
  box-shadow: 0 6px 30px rgba(102, 126, 234, 0.6);
}

.chat-button i {
  font-size: 28px;
  position: relative;
  z-index: 2;
}

/* 脉冲动画 */
.pulse-ring {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  animation: pulse 2s ease-out infinite;
  opacity: 0.6;
}

@keyframes pulse {
  0% {
    transform: scale(1);
    opacity: 0.6;
  }
  50% {
    transform: scale(1.3);
    opacity: 0.3;
  }
  100% {
    transform: scale(1.5);
    opacity: 0;
  }
}

/* 聊天窗口样式 */
.chat-window {
  position: fixed;
  bottom: 30px;
  right: 30px;
  width: 400px;
  height: 600px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* 聊天窗口头部 */
.chat-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 16px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  user-select: none;
}

.header-content {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header-content i {
  font-size: 24px;
}

.header-title {
  font-size: 16px;
  font-weight: 600;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.minimize-btn,
.close-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  color: white;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}

.minimize-btn:hover,
.close-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.minimize-btn i,
.close-btn i {
  font-size: 16px;
}

/* 聊天内容区域 */
.chat-content {
  flex: 1;
  position: relative;
  background: #f5f5f5;
}

.chat-iframe {
  width: 100%;
  height: 100%;
  border: none;
}

/* 加载动画 */
.loading-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(255, 255, 255, 0.95);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

.loading-spinner {
  text-align: center;
  color: #667eea;
}

.loading-spinner i {
  font-size: 40px;
  display: block;
  margin-bottom: 12px;
  animation: spin 1s linear infinite;
}

.loading-spinner span {
  font-size: 14px;
  color: #666;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

/* 动画效果 */
.bounce-enter-active {
  animation: bounce-in 0.5s;
}

.bounce-leave-active {
  animation: bounce-out 0.3s;
}

@keyframes bounce-in {
  0% {
    transform: scale(0);
    opacity: 0;
  }
  50% {
    transform: scale(1.1);
  }
  100% {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes bounce-out {
  0% {
    transform: scale(1);
    opacity: 1;
  }
  100% {
    transform: scale(0);
    opacity: 0;
  }
}

.slide-fade-enter-active {
  animation: slide-fade-in 0.3s ease-out;
}

.slide-fade-leave-active {
  animation: slide-fade-out 0.3s ease-in;
}

@keyframes slide-fade-in {
  0% {
    transform: translateY(20px);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}

@keyframes slide-fade-out {
  0% {
    transform: translateY(0);
    opacity: 1;
  }
  100% {
    transform: translateY(20px);
    opacity: 0;
  }
}

/* 响应式设计 */
@media (max-width: 768px) {
  .chat-window {
    width: calc(100vw - 40px);
    height: calc(100vh - 100px);
    bottom: 20px;
    right: 20px;
  }

  .chat-button {
    bottom: 20px;
    right: 20px;
    width: 56px;
    height: 56px;
  }

  .chat-button i {
    font-size: 24px;
  }
}

@media (max-width: 480px) {
  .chat-window {
    width: 100vw;
    height: 100vh;
    bottom: 0;
    right: 0;
    border-radius: 0;
  }

  .chat-button {
    bottom: 16px;
    right: 16px;
  }
}
</style>

